package com.yanghui.dtracing.plugin.thread;

import com.yanghui.dtracing.core.match.ClassMatch;
import com.yanghui.dtracing.core.match.IndirectMatch;
import com.yanghui.dtracing.core.plugin.AbstractClassEnhancePluginDefine;
import com.yanghui.dtracing.core.plugin.IConstructorMethodInterceptPoint;
import com.yanghui.dtracing.core.plugin.IInstanceMethodInterceptPoint;
import net.bytebuddy.description.annotation.AnnotationDescription;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatchers;

import java.util.concurrent.Callable;

public class ThreadDefinition extends AbstractClassEnhancePluginDefine {

    private static final String TRACE_CROSS_THREAD_ANNOTATION = "com.yanghui.dtracing.common.annotation.TraceCrossThread";

    @Override
    public ClassMatch enhanceClass() {
        return new IndirectMatch() {
            @Override
            public ElementMatcher.Junction buildJunction() {
                return ElementMatchers.isAnnotatedWith(ElementMatchers.named(TRACE_CROSS_THREAD_ANNOTATION))
                        .and(ElementMatchers.isSubTypeOf(Runnable.class).or(ElementMatchers.isSubTypeOf(Callable.class)));
            }

            @Override
            public boolean isMatch(TypeDescription typeDescription) {
                for(AnnotationDescription annotationDescription : typeDescription.getInheritedAnnotations()){
                    String annotationName = annotationDescription.getAnnotationType().getName();
                    if(TRACE_CROSS_THREAD_ANNOTATION.equals(annotationName)){
                        for(TypeDescription.Generic generic : typeDescription.getInterfaces()){
                            if(Runnable.class.getName().equals(generic.getTypeName()) ||
                                    Callable.class.getName().equals(generic.getTypeName())){
                                return true;
                            }
                        }
                    }
                }
                return false;
            }
        };
    }

    @Override
    public IConstructorMethodInterceptPoint[] buildConstructorMethodInterceptPoints() {
        return new IConstructorMethodInterceptPoint[]{
            new IConstructorMethodInterceptPoint(){
                @Override
                public ElementMatcher<MethodDescription> buildConstructorMatcher() {
                    return ElementMatchers.any();
                }

                @Override
                public String constructorInterceptor() {
                    return "com.yanghui.dtracing.plugin.thread.ThreadConstructorInter";
                }
            }
        };
    }

    @Override
    public IInstanceMethodInterceptPoint[] buildInstanceMethodInterceptPoints() {
        return new IInstanceMethodInterceptPoint[]{
            new IInstanceMethodInterceptPoint(){
                @Override
                public ElementMatcher<MethodDescription> buildMethodsMatcher() {
                    return ElementMatchers.named("run").or(ElementMatchers.named("call"));
                }

                @Override
                public String methodInterceptor() {
                    return "com.yanghui.dtracing.plugin.thread.ThreadInstanceMethodAroundInterceptor";
                }
            }
        };
    }
}
